package com.controller;

import cn.hutool.core.date.DateUtil;
import com.annotation.IgnoreAuth;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.entity.ChongdianzhuangEntity;
import com.entity.ChongdianzhuangOrderEntity;
import com.entity.view.ChongdianzhuangView;
import com.service.ChongdianzhuangOrderService;
import com.service.ChongdianzhuangService;
import com.utils.MPUtil;
import com.utils.NbUtil;
import com.utils.PageUtils;
import com.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Map;

@RestController
@RequestMapping("/chongdianzhuangorder")
public class ChongdianzhuangOrderController {
    @Autowired
    private ChongdianzhuangOrderService chongdianzhuangOrderService;
    @Autowired
    private ChongdianzhuangService chongdianzhuangService;

    @RequestMapping("/aiAdd")
    @IgnoreAuth
    public R aiAdd(@RequestBody Integer number){
        String s = chongdianzhuangService.aiAdd(number);
        return R.ok().put("data", s);
    }

    /**
     * 占用充电桩
     */
    @Transactional
    @RequestMapping("/add")
    public R add(@RequestBody ChongdianzhuangOrderEntity chongdianzhuangOrder) {
        ChongdianzhuangEntity chongdianzhuang = new ChongdianzhuangView();
        chongdianzhuang.setDianzhuangbianhao(chongdianzhuangOrder.getDianzhuangbianhao());
        EntityWrapper< ChongdianzhuangEntity> ew = new EntityWrapper< ChongdianzhuangEntity>();
        ew.allEq(MPUtil.allEQMapPre( chongdianzhuang, "chongdianzhuang"));
        ChongdianzhuangView chongdianzhuangView =  chongdianzhuangService.selectView(ew);
        if (chongdianzhuangView == null) {
            return R.error("充电桩不存在");
        }
        if (!"空闲中".equals(chongdianzhuangView.getStatus())) {
            return R.error("充电桩已经被占用或维修中");
        }
        chongdianzhuangView.setStatus("使用中");
        chongdianzhuangService.updateById(chongdianzhuangView);
        chongdianzhuangOrder.setIsstop(Boolean.FALSE);
        if (chongdianzhuangOrder.getStarttime() == null) {
            chongdianzhuangOrder.setStarttime(LocalDateTime.now());
        }
        chongdianzhuangOrder.setOrderid(chongdianzhuangOrder.getStarttime().toString() + "-" + chongdianzhuangOrder.getDianzhuangbianhao() );
        chongdianzhuangOrderService.insert(chongdianzhuangOrder);
        return R.ok().put("data",chongdianzhuangOrder);
    }
    /**
     * 后端列表
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params, ChongdianzhuangOrderEntity chongdianzhuang,
                  HttpServletRequest request){
        EntityWrapper<ChongdianzhuangOrderEntity> ew = new EntityWrapper<ChongdianzhuangOrderEntity>();
        PageUtils page = chongdianzhuangOrderService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, chongdianzhuang), params), params));

        return R.ok().put("data", page);
    }

    /**
     * 前端列表
     */
    @IgnoreAuth
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,ChongdianzhuangOrderEntity chongdianzhuang,
                  HttpServletRequest request){
        EntityWrapper<ChongdianzhuangOrderEntity> ew = new EntityWrapper<ChongdianzhuangOrderEntity>();
        PageUtils page = chongdianzhuangOrderService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, chongdianzhuang), params), params));
        return R.ok().put("data", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/lists")
    public R list( ChongdianzhuangOrderEntity chongdianzhuang){
        EntityWrapper<ChongdianzhuangOrderEntity> ew = new EntityWrapper<ChongdianzhuangOrderEntity>();
        ew.allEq(MPUtil.allEQMapPre( chongdianzhuang, "chongdianzhuang"));
        return R.ok().put("data", chongdianzhuangOrderService.selectListView(ew));
    }

    /**
     * 查询
     */
    @RequestMapping("/query")
    public R query(ChongdianzhuangOrderEntity chongdianzhuang){
        EntityWrapper< ChongdianzhuangOrderEntity> ew = new EntityWrapper< ChongdianzhuangOrderEntity>();
        ew.allEq(MPUtil.allEQMapPre( chongdianzhuang, "chongdianzhuang"));
        ChongdianzhuangOrderEntity ChongdianzhuangOrderEntity =  chongdianzhuangOrderService.selectView(ew);
        return R.ok("查询充电桩成功").put("data", ChongdianzhuangOrderEntity);
    }

    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        ChongdianzhuangOrderEntity chongdianzhuang = chongdianzhuangOrderService.selectById(id);
        return R.ok().put("data", chongdianzhuang);
    }

    /**
     * 前端详情
     */
    @IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id){
        ChongdianzhuangOrderEntity chongdianzhuang = chongdianzhuangOrderService.selectById(id);
        return R.ok().put("data", chongdianzhuang);
    }

}
